﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jumpton.Persistent;
using Jumpton.Data;
using Jumpton.SFA.Server.Entity;

namespace Jumpton.SFA.Server.BusinessRule.CallRoute
{
    public class VisitManager
    {

        public DataSet GetVisitPlan(DateTime date, decimal userId)
        {
            return SqlData.OpenSP("SP_GET_ROUTE_CUSTOMERS", userId, BusinessLogic.GetDateTimeString(date));
        }

        public DataSet GetMyVisitPlan(string StartDate, string EndDate, decimal userId)
        {
            //where (start_time between '{0}' and '{1}' or plan_start_time between '{0}' and '{1}') and user_id={2} order by t1.SEQUENCE "
            string strSql = String.Format(@"select t1.*,t2.name,t2.code,DI.NAME AS CUSTOMERTYPE from visits t1
                inner join customers t2
                on t1.customer_id=t2.id 
                left join dbo.DictionaryItems DI on DI.DictionaryItemID=t2.CUST_CLASS_CODE 
                where t1.LAST_UPDATE_TIME>'{0}' and user_id={1} order by t1.LAST_UPDATE_TIME desc "
                , StartDate, userId);

            return SqlData.OpenSql(strSql);
        }

        public bool UpdateRouteCustomers(decimal USER_ID, int ROUTE_FORCE, string CUSTOMERS, int ROUTENUMBER, String VISITDATE, Boolean ACTIVE)
        {
            return SqlData.ExecuteSP("[SP_SAVE_ROUTE_CUSTOMERS]", USER_ID, ROUTE_FORCE, CUSTOMERS, ROUTENUMBER, VISITDATE, ACTIVE) >= 0;
        }

        //        public DataTable GetVisitRecord(DateTime date, decimal userId, decimal DomainID)
        //        {
        //            return SqlData.OpenSP("sp_selectVisitPlanByMonth",date,userId,DomainID).Tables[0];
        //        }



        //        public DataTable GetMyVisitPlan(DateTime StartDate,DateTime EndDate, decimal userId)
        //        {
        //            return SqlData.OpenSql(String.Format(@"select t1.*,t2.name,t2.code from visits t1
        //                inner join customers t2
        //                on t1.customer_id=t2.id 
        //                where start_time between '{0}' and '{1}' and user_id={2}", StartDate.Date, EndDate.Date.AddDays(1).AddMinutes(-1), userId)).Tables[0];
        //        }


        public bool Save(VISITS visits)
        {
            if (visits != null)
            {
                visits.LAST_UPDATE_TIME = decimal.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
                visits.Save();
                return true;
            }
            else
                return false;
        }

        public ObjectTable GetVisitRoute(string USERID, decimal DOMAINID, string date)
        {
            if (date == "")
                date = DateTime.Now.ToString("yyyyMMdd");
            else
                date = date.Replace("-", "").Replace("/", "");

            return new ObjectTable(typeof(VISITS_ROUTE_VIEW), " V.DOMAIN_ID=" + DOMAINID + " AND V.USER_ID=" + USERID + " AND SUBSTRING(CAST(V.START_TIME AS VARCHAR),0,9)='" + date + "' ORDER BY START_TIME ");
        }


        public DataTable GetExportVisitBaseInfo(decimal userId, decimal DomainID, String startTime, String endTime)
        {

            return SqlData.OpenSP("sp_exportBaseVisitInfo", userId, DomainID, startTime, endTime).Tables[0];
        }

        public DataTable GetExportVisitBaseInfoKong(string userIds, decimal DomainID, String startTime, String endTime)
        {

            return SqlData.OpenSP("sp_exportBaseVisitInfo_Kong", userIds, DomainID, startTime, endTime).Tables[0];
        }

        public DataTable GetExportVisitBaseInfo(String visitId)
        {

            String sql = string.Format(@"  select b.ID, b.NAME AS BRAND_NAME,c.VISITID,c.VALUE AS TASTE,d.VALUE AS [TYPES] from BRANDS B,CPR_BRAND_DATAS C  ,CPR_BRAND_DATAS D
	                                        where b.ID = c.BRAND_ID
                                            and c.BRAND_CONFIG_ID=28
                                            and D.VISITID=c.VISITID and d.BRAND_CONFIG_ID=1
                                            and d.BRAND_ID = b.ID 
                                            and c.VISITID='{0}'",visitId);
            return SqlData.OpenSql(sql).Tables[0];
        }
    }
}
